<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_Controller {
// Some code happens here
    public function __construct()
    {
        parent:: __construct();
        $this->loadTemplate();
        $this->load->helper("url");
	    $this->load->model('sanpham');
        $this->load->model('binhluan');
        $this->load->library('url'); 
        $this->load->library('utils');   
        $this->load->helper('html');
		$this->load->helper('text');
	    $this->load->library('parser');
        $this->load->library('pagination');
        $this->load->library('currency');
    }
    
	public function index()
	{
	   $this->home_page();
	}
   
    //Lay template cua 1 sản phẩm
    public function render_post($data)
    {
        $data['gia'] = $this->currency->convert_to_vnd($data['gia']);
        return $this->parser->parse('main/product_item_simp',$data,true);
    }
    
    //Lấy thông tin tootip
    public function view_tooltip($data)
    {
        $data['gia'] = $this->currency->convert_to_vnd($data['gia']);
        return $this->parser->parse('main/tooltip',$data,true);
    }
    
    //Xây dựng 1 tooltip
    public function tooltip($id)
    {
        $product_id = $id;//$_GET['id'];   
        $tooltip_db['id'] = $product_id;
        $tooltip_db['name'] = $this->sanpham->get_product_name($product_id);
        $tooltip_db['price'] =$this->sanpham->get_product_prices($product_id);
        for($i=1 ; $i <=11 ; $i++)
        {
            $tooltip_db['info'.$i] = $this->sanpham->get_product_info($product_id,$i);
        }
        $this->load->view('main/tooltip',$tooltip_db);
    }
    //Load dau tien;

    function home_page()
    {
        $menu['active1'] = "active";
        $this->template->parse_view('menu','main/menu',$menu,true);
        $this->template->parse_view('navbars','main/navbars',null,true);
        $this->template->render();
        
    }
    //Load danh sach dua vao category
    function load_product_category($maincategory,$category,$offset = 0,$perpage = 20)
    {
        $this->post['html'] = '';
		$config['base_url'] = site_url('home/load_product_category/'.$maincategory.'/'.$category);
		$config['total_rows'] = $this->sanpham->get_num_product_category($maincategory,$category);
	    $config['per_page'] = $perpage;
        $config['cur_page'] = $offset/20;
        
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';
	    $this->pagination->initialize($config);
        //Du lieu rent paging
        $pdata['html'] = $this->pagination->create_links();
        $arrProducts = $this->sanpham->get_product_category($maincategory,$category,$perpage,$offset);
        if($arrProducts != null)
        {
        foreach($arrProducts as $product)
            {   $data['thumb'] = $this->sanpham->get_images($product->ID,true);
                $data['id'] = $product->ID;
                $data['name']=$product->TenSP;
                $data['gia']=$product->Gia;
                $data['duongdan']= 'san-pham/p'.$this->url->get_friendly_url($product->ID,$product->TenSP);
                $data['tooltip'] = $this->view_tooltip($data);
                $this->post['html'].= $this->render_post($data);//"hello there ! this is the fucking content";       
            }
        }
        else
        {
            $this->post['html'] = "Dữ Liệu Đang Cập Nhật...";
        }
        $pdata['category'] = $maincategory.'/'.$category;
        $html = $this->parser->parse('main/paging',$pdata,true);
        $html2 = $this->parser->parse('home/content',$this->post,true);
        echo $html2.$html; 
    }
    
    
    function detail($idSP)
    {
        /*compare*/
         $data['compare']='';
        if(isset($this->session->userdata['items'])&&$this->session->userdata['items']!='')
        {
        $strproduct_id = $this->session->userdata['items'];
        if($strproduct_id!='')
        {
        $this->load->model('sanpham');
            $arrProducts= explode(',',$strproduct_id);
            foreach($arrProducts as $product){
                $product_db['id'] = $product;
                $product_db['name'] = $this->sanpham->get_product_name($product);
                $data['temp'][$product]=$product;
                $data['compare'].='<li class="com'.$product_db['id'].'"><a href="javascript:void(0)" onclick="deleteCompare('.$product_db['id'].')">'.$product_db['name'].'</a></li>';
            }
        }
         }
         else{
             $data['compare']='<li class="null_comp">Vui lòng chọn sản phẩm để so sánh</li>';
        }
            //$this->template->write_view('compare','test/content',$data);
        //$data['compare']=$this->parser->parse('compare/main',$product_db,true);
            /*compare*/
        $data['header'] =  $this->parser->parse('main/header','',true);
        $data['footer'] =  $this->parser->parse('main/footer','',true);
        $data['menu'] =  $this->parser->parse('main/menu','',true);
        //$data['compare'] =  $this->parser->parse('compare/main','',true);
        $result = $this->sanpham->get_detail($idSP);
        //print_r($result['thongso']);
        $data['ten'] = $result['TenSP'];
        $data['Available'] = ($result['available'] =="1"?"Sản phẩm này còn hàng":"Sản phẩm này hết hàng");
        $data['gia'] = $this->currency->convert_to_vnd($result['price']);
        $data['linkIMG'] = $this->utils->get_url_img($result['large_images']['0']['imageurl']) ; 
        $thongso = "";
        $arrInfo = $this->sanpham->get_info_id($idSP);
        //var_dump($arrInfo);
        foreach($arrInfo as $info)
        {
            $data['info'][$info->id].= '<td class="infoname">'.$info->Ten.' :</td>';
        }
        foreach($arrInfo as $inf)
        {
            $data['info'][$inf->id].= '<td>'.$this->sanpham->get_product_info($idSP,$inf->id).'</td>';
        }
       /* foreach($result['thongso'] as $ts)
        {
            $thongso .= $this->parser->parse('chitiet/thongso',$ts,true);
        }*/
        
        
        $tuongtu="";// $result['thongso'];
        $arrTuongtu = $this->sanpham->get_like_product($idSP,4);
        $arrInfo = $this->sanpham->get_info_id($idSP);
        //var_dump($arrInfo);
        foreach($arrInfo as $info)
        {
            $data['info'][$info->id].= '<td class="infoname">'.$info->Ten.' :</td>';
        }
        foreach($arrInfo as $inf)
        {
            $data['info'][$inf->id].= '<td>'.$this->sanpham->get_product_info($idSP,$inf->id).'</td>';
        }
       /* foreach($result['thongso'] as $ts)
        {
            $thongso .= $this->parser->parse('chitiet/thongso',$ts,true);
        }*/
        foreach($arrTuongtu as $product)
        {
            
                $data['id'] = $product['id'];
                $data['name']=$product['TenSP'];
                $data['gia']=$this->currency->convert_to_vnd($this->sanpham->get_product_prices($product['id']));
                $data['thumb']=$this->sanpham->get_images($product['id'],true);
                $data['duongdan']= 'san-pham/p'.$this->url->get_friendly_url($product['id'],$product['TenSP']);
            $tuongtu.=$this->parser->parse('chitiet/product_item',$data,true);
        }
        $data['thongso'] = $thongso;
        $data['tuongtu'] = $tuongtu;
        
        
           //Hot Product
          $data['hot_product'] =""; 
          $item = "";
          $products = $this->sanpham->get_hot_product(10);
            foreach($products as $product)
            {
                $product['gia'] = $this->currency->convert_to_vnd($this->sanpham->get_product_prices($product['ID']));
                $product['image'] = $this->utils->get_url_img($this->sanpham->get_images($product['ID'],true));
                $product['link']= 'san-pham/p'.$this->url->get_friendly_url($product['ID'],$product['TenSP']); 
                $item .= $this->parser->parse('home/very_simpl_product',$product,true);
            }
           $data['hot_product'] = $item; 
        
   $data['binhluan'] = ""  ; 
        $comts = $this->binhluan->get_all($idSP); 
        $data_comment['id'] = $idSP;
        $data_comment['list_comment'] = "";
        foreach($comts as $comment)
        {
            $cmt['name'] = $comment->Name;
            $cmt['ngaytao'] = $comment->NgayTao;
            $cmt['noidung'] = $comment->text;
            $data_comment['list_comment'] .= $this->parser->parse('comment/a_comment',$cmt,true);
        }  
         
        $data['binhluan'] = $this->parser->parse('chitiet/binhluan',$data_comment,true);
        return $this->parser->parse('chitiet/index',$data,false);
                
    }
    
  
  
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */